'use strict';

const BaseController = require('./base');

class LeaseController extends BaseController {
  get _service() {
    return 'lease';
  }

  async show() {
    await this._show('获取租约失败');
  }

  async index() {
    const { ctx } = this;
    const { status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;

    const tempResult = await this._getService().findList({
      status,
      prop,
      sort,
      pageIndex,
      pageSize,
      createdAt,
    });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取租约失败');
    }
  }

  async indexMy() {
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.params();

    const result = await this._getService().findList({
      uid: ctx.userId,
      pageIndex,
      pageSize,
    });

    if (result) {
      this.success(result.data, result.count);
    } else {
      this.error('获取租约失败');
    }
  }

  async indexFromMy() {
    const { ctx } = this;
    const { houseId, status, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;

    const tempResult = await this._getService().findList({
      houseId,
      status,
      oid: ctx.userId,
      prop,
      sort,
      pageIndex,
      pageSize,
      createdAt,
    });
    const result = ctx.helper.flat(tempResult.data);
    if (result) {
      this.success(result, tempResult.count);
    } else {
      this.error('获取租约失败');
    }
  }

}

module.exports = LeaseController;
